package com.example.backend.controller.login;

import com.example.backend.dto.RegisterDto;
import com.example.backend.service.mysql.user.UserRegisterService;
import com.example.backend.utils.ResultJson;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/register")
@Tag(name = "用户注册", description = "用户注册接口")
public class RegisterController {

    private final PasswordEncoder passwordEncoder;
    private final UserRegisterService userRegisterService;


    @Operation(summary = "密码注册", description = "用户名密码注册", tags = {"注册接口"})
    @PostMapping("/register")
    public ResultJson<?> register(@Valid @RequestBody RegisterDto registerDto) {
        if (!registerDto.getPassword().equals(registerDto.getRepeatPassword())) {
            return ResultJson.fail("两次密码不一致");
        }
        registerDto.setPassword(passwordEncoder.encode(registerDto.getPassword()));
        userRegisterService.passwordRegister(registerDto);
        return ResultJson.ok();
    }
}
